//
//  LocationBottomView.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/27.
//  Copyright © 2020 tym. All rights reserved.
//

#import "LocationBottomView.h"
#import "UIView+ZFFrame.h"
#import "Sizes.h"
#import "LocationBottomCell.h"
#import "UIButton+Gradient.h"
#import "TYMBaseTableView.h"

@interface LocationBottomView()

@property(nonatomic,weak) IBOutlet TYMBaseTableView *tableView;
@property(nonatomic,weak) IBOutlet UIButton *soureBtn;

@property(nonatomic,assign) NSInteger indexPath;
@end

@implementation LocationBottomView


-(void)awakeFromNib{
    [super awakeFromNib];
    [self.soureBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    self.soureBtn.layer.masksToBounds = YES;
    self.soureBtn.layer.cornerRadius = 5;
    [self.tableView registerNib:[UINib nibWithNibName:@"LocationBottomCell" bundle:nil] forCellReuseIdentifier:[LocationBottomCell identifier]];
}

+(LocationBottomView*)getLocationBottomView{
    
    return  [[[NSBundle mainBundle] loadNibNamed:@"LocationBottomView" owner:self options:nil] lastObject];
}

-(void)setDataArray:(NSArray *)dataArray{
    
    _dataArray = dataArray;
    _indexPath = 0;
    [self.tableView reloadData];

}

-(IBAction)selectAddressAction:(id)sender{
    if (self.seleIndexBlock) {
        self.seleIndexBlock(self.dataArray[_indexPath]);
    }
}

#pragma  mark - tableView 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [LocationBottomCell cellForHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LocationBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:[LocationBottomCell identifier] forIndexPath:indexPath];
    cell.poiModel = self.dataArray[indexPath.row];
    if (indexPath.row == _indexPath) {
        cell.seleImage.hidden = NO;
    }else{
        cell.seleImage.hidden = YES;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _indexPath = indexPath.row;
    [tableView reloadData];
    
    if (self.tableSeleBlock) {
        self.tableSeleBlock(self.dataArray[_indexPath]);
    }
}

@end
